<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>管理学生用户</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="<%=basePath%>layui/css/layui.css" media="all">
  
<script src="<%=basePath%>/js/jquery-3.2.1.min.js"></script>
</head>
<body>
<iframe style="width:100%;height:100%;display:none;" id="add_page" src="/bysjxksys/pages/admin/addStu.jsp" frameborder="no" border="0" marginwidth="0" marginheight="0" scrolling="no" allowtransparency="yes"></iframe>
<iframe style="width:100%;height:100%;display:none;" id="edit_page" src="/bysjxksys/pages/admin/editStu.jsp" frameborder="no" border="0" marginwidth="0" marginheight="0" scrolling="no" allowtransparency="yes"></iframe>

<form class="layui-form">
	<div class="layui-form-item" id="list_page" style="margin-top:30px;">
	    <div class="layui-inline">
		    <label class="layui-form-label">学号：</label>
		    <div class="layui-input-inline">
			    <input class="layui-input" type = "text" id="account">
			</div>
		</div>
	    <div class="layui-inline">
		    <label class="layui-form-label">姓名：</label>
		    <div class="layui-input-inline">
			    <input  class="layui-input" type = "text" id="username">
			</div>
		</div>
	    <div class="layui-inline" style="margin-right:10px;">
			<button type="button" class="layui-btn search_btn" data-type="reload">查询</button>
			<button type="button" class="layui-btn" onclick="add();">添加</button>
		</div>
 	 </div>
 	 <div style="margin-left:10px; margin-right:10px;">
		<table id="userlist" lay-filter="userlist"></table>
	 </div>
</form>

<script type="text/html" id="caozuo">
  <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

</body>


<script src="<%=basePath%>/js/vue.min.js"></script>
<script src="<%=basePath%>/layui/layui.js" charset="utf-8"></script>
<script>
layui.use('table', function() {
	var table = layui.table;
	
	table.render({
		elem : '#userlist',
	    height:450,
		// width: 800,
		page: true,
		limit: 10,
		url : '/bysjxksys/admin/stu_list',
		where:{
			account:  $('#account').val(),
			username: $('#username').val()
		},
		response: {
			statusName: 'code'   //规定数据状态的字段名称，默认：code
			,statusCode: 0       //规定成功的状态码，默认：0
			,msgName:   'msg'    //规定状态信息的字段名称，默认：msg
			,countName: 'count'  //规定数据总数的字段名称，默认：count
			,dataName:  'data'   //规定数据列表的字段名称，默认：data
		},
		cols: [[ //标题栏
			{field: '', title: '序号', width:40, type:'numbers'}
		    ,{field: 'sno', title: '学号', align:'center'}
		    ,{field: 'sname', title: '姓名', align:'center'}
		    ,{field: 'sex', title: '性别', align:'center'}
		   ,{field: 'sPassword', title: '密码', align:'center'}
		   ,{field: 'sDepartment', title: '系部', align:'center'}
		   ,{field: 'sClass', title: '班级', align:'center'}
		   ,{field: 'email', title: '邮件', align:'center'}
		   ,{field: 'sPhone', title: '电话', align:'center'}
		   ,{title : '操作', toolbar: '#caozuo', align: 'center'}
		]],
        id : "userlistTable"
    });

    table.on('tool(userlist)', function(obj){
    	var data = obj.data;
    	if(obj.event === 'edit'){
    		localStorage.setItem('stuId',data.stuId);
    		localStorage.setItem('sno',data.sno);
    		localStorage.setItem('sname',data.sname);
    		localStorage.setItem('sex',data.sex);
    		localStorage.setItem('sPassword',data.sPassword);
    		localStorage.setItem('sDepartment',data.sDepartment);
    		localStorage.setItem('sClass',data.sClass);
    		localStorage.setItem('email',data.email);
    		localStorage.setItem('sPhone',data.sPhone);
    		
    		layer.open({
                type: 2,
                title : '修改学生信息',
                //skin: 'layui-layer-rim', //加上边框
                area: ['800px', '600px'], //宽高
                content:'/bysjxksys/pages/admin/editStu.jsp',
                btn:['确定','取消'],
                yes:function(index,layero){
                    var formSubmit = layer.getChildFrame('form',index);
                    var submited = formSubmit.find('button')[0];
                    submited.click();
                    location.reload();
                }
            });
    
    	}else if(obj.event === 'del'){
            layer.confirm('确定删除此用户？',{icon:3, title:'提示信息'},function(index){
        		var param = {};
        		param.sno = data.sno;
            	
        		$.ajax({
        			type:"GET",
        			url:"/bysjxksys/admin/del_stu",
        			dataType:"json",
        			//contentType:"application/json",
        	        data:param,
        			success: function(data){
            			layer.close(index);
        				if(data.code==1){
        					alert(data.msg);
        					window.location.reload();
        				}else{
        					alert("删除用户成功！");
        					top.location.reload();				
        				}
        			},
        			error: function(data, type, err){
            			layer.close(index);
        				console.log(type);
        				console.log(err);
        			}
        		});
            });
    	}
   	});

    $(".search_btn").on("click",function(){
		table.reload("userlistTable",{
			page: {
				curr: 1 //重新从第 1 页开始
			},
			where:{
				account:  $('#account').val(),
				username: $('#username').val()
			}
		});
	});
}); 

function add(){
	  layer.open({
          type: 2,
          title : '新增学生',
          //skin: 'layui-layer-rim', //加上边框
          area: ['800px', '600px'], //宽高
          content:'/bysjxksys/pages/admin/addStu.jsp',
          btn:['确定','取消'],
          yes:function(index,layero){
              var formSubmit = layer.getChildFrame('form',index);
              var submited = formSubmit.find('button')[0];
              submited.click();
              location.reload();
          }
      });
}
</script>
</html>